import { defineStore } from "pinia";
import { watch } from "vue";
import { LockScreenState } from "@/stores/interface";
import { useIdle } from "@vueuse/core";
import piniaPersistConfig from "@/config/piniaPersist";

// 长时间不操作默认锁屏时间
const initTime = 60 * 60 * 1000; // 60 分钟
const { idle } = useIdle(initTime); // 5 min
export const useLocksSreenStore = defineStore({
  id: "lockscreen",
  state: (): LockScreenState => ({
    isLock: false, //控制锁屏是否显示
    lockPwd: "" //锁屏密码
  }),
  getters: {},
  actions: {
    // 改变锁屏状态
    setLock(lock: boolean) {
      this.isLock = lock;
      if (!lock) {
        this.resetLockPwd();
      }
    },
    /** 重置锁屏密码 */
    resetLockPwd() {
      this.lockPwd = "";
    }
  },
  persist: piniaPersistConfig("lockscreen")
});
watch(idle, idleValue => {
  if (route.name === "login") {
    setLock(false);
    return;
  }
  if (idleValue) {
    setLock(true);
  }
});
